﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using STT.Business;
using STT.Entities;
using System.IO;
using STT.Layer.Connection;


namespace STT.Admin.UserControls
{
    public partial class ucTKB : System.Web.UI.UserControl
    {
        ToolBox tb = new ToolBox();
        public bool chonSuaClick;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlKhoaHoc.DataSource = tb.GetDataTableByNguoiTao("TbKhoaHoc_SelectAll", Session["TenDN"].ToString());
                ddlKhoaHoc.DataTextField = "TenKhoaHoc";
                ddlKhoaHoc.DataValueField = "MaKhoaHoc";
                ddlKhoaHoc.DataBind();

                ddlKhoaHoc.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
                ddlKhoaHoc.SelectedIndex = 0;
                
                LoadTKB();
                LoadTKBTam();
            }
            Loanddl();

            if (Request.QueryString["idtkb"] != null)
            {
                int IdTKB = int.Parse(Request.QueryString["idtkb"]);

                // Lay thong tin thoi khoa bieu sua
                ETbTKB tkb = BTbTKB.SelectByID(IdTKB);

                ThongBao(tkb.NgayHoc.ToString());
                //ddlCaHoc. = tkb.CaHoc.ToString();
            }
        }

        public void Loanddl()
        {
            DataTable dtCa = BTbTKB.SelectNoCaHoc(txtNgayHoc.Text);
            //ThongBao(dtCa.ToString());
            if (dtCa.Rows.Count==0)
            {
                ddlCaHoc.DataTextField = "";
                ddlCaHoc.DataValueField = "";
                ddlCaHoc.DataBind();
                ddlCaHoc.Items.Clear();
                ddlCaHoc.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
                ddlCaHoc.SelectedIndex = 0;
            }
            else
            {
                ddlCaHoc.DataSource = dtCa;
                ddlCaHoc.DataTextField = "TenCa";
                ddlCaHoc.DataValueField = "MaCa";
                ddlCaHoc.DataBind();
                ddlCaHoc.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
                ddlCaHoc.SelectedIndex = 0;
            }
            
            ddlGiangVien.DataSource = tb.GetDataTableByNguoiTao("TbGiangVien_SelectAll", Session["TenDN"].ToString());
            ddlMonHoc.DataSource = BTbMonHoc.SelectAll(Session["TenDN"].ToString());
            ddlPhongHoc.DataSource = BTbTKB.TbTKB_SelectAll_PhongHoc();

            ddlGiangVien.DataTextField = "TenGV";
            ddlPhongHoc.DataTextField = "TenPH";
            ddlMonHoc.DataTextField = "TenMonHoc";

            ddlGiangVien.DataValueField = "MaGV";
            ddlMonHoc.DataValueField = "MaMonHoc";
            ddlPhongHoc.DataValueField = "MaPH";
            
            ddlPhongHoc.DataBind();
            ddlMonHoc.DataBind();
            ddlGiangVien.DataBind();

            ddlPhongHoc.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddlPhongHoc.SelectedIndex = 0;

            ddlMonHoc.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddlMonHoc.SelectedIndex = 0;

            ddlGiangVien.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddlGiangVien.SelectedIndex = 0;

        }

        void LoadTKB()
        {
            gvTKB.AutoGenerateColumns = false;
            gvTKB.DataSource = BTbTKB.SelectAllByIdClass(ddlLop.SelectedValue);
            gvTKB.DataBind();
        }

        void LoadTKBTam()
        {
            gvTKBtam.AutoGenerateColumns = false;
            gvTKBtam.DataSource = BTbTKB.SelectAllByIdClassNot(ddlLop.SelectedValue);
            gvTKBtam.DataBind();
        }
       
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }
        protected void gvTKB_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvTKB.EditIndex = e.NewEditIndex;
            gvTKB.DataBind();
        }

        protected void gvTKB_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvTKB.EditIndex = -1;
            gvTKB.DataBind();
        }

        protected void lbXoaTat_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < gvTKB.Rows.Count; i++)
                {
                    CheckBox cbChon = (CheckBox)gvTKB.Rows[i].Cells[0].FindControl("cbChon");
                    if (cbChon != null && cbChon.Checked)
                    {
                        BTbTKB.Delete(int.Parse(gvTKB.DataKeys[i].Value.ToString()));
                    }
                    else
                    {
                        ThongBao("Bạn chưa chọn thời khóa biểu nào để xóa!");
                    }
                }

                LoadTKB();
                Session["action"] = 3;
            }
            catch
            {
                ThongBao("Dữ liệu liên quan đến thời khóa biểu này vẫn tồn tại, vui lòng kiểm tra lại dữ liệu!");
            }
        }

        // Phân trang
        protected void gvTKB_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTKB.PageIndex = e.NewPageIndex;
            gvTKB.DataBind();
        }
        public static String maTKB;

        protected void gvTKB_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ChonSua")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                string maTT =  gvTKB.DataKeys[index].Values[0].ToString();

                Response.Redirect("~/Admin/Default.aspx?idpage=ttkb&idtkb=" + maTT);
            }
        }


        protected void btImport_Click(object sender, EventArgs e)
        {

        }
        protected void btThemExcel_Click(object sender, EventArgs e)
        {

        }

        protected void ddlKhoaHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlLop.DataSource = BTbLop.SelectTuKhoaHoc(int.Parse(ddlKhoaHoc.SelectedValue));
            ddlLop.DataTextField = "TenLop";
            ddlLop.DataValueField = "MaLop";
            ddlLop.DataBind();

            ddlLop.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddlLop.SelectedIndex = 0;
        }

        protected void ddlLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadTKB();
            LoadTKBTam();
            Loanddl();
        }

        protected void txtNgayHoc_TextChanged(object sender, EventArgs e)
        {
            ddlGiangVien.Enabled = true;
            ddlKhoaHoc.Enabled = true;
            ddlMonHoc.Enabled = true;
            ddlCaHoc.Enabled = true;
            ddlPhongHoc.Enabled = true;
            btThemMoiTKB.Enabled = true;
            Loanddl();
        }

        bool KiemTraDuLieu()
        {
          
                return BTbTKB.KiemTraDuLieu(new ETbTKB
                {
                    MaMonHoc = int.Parse(ddlMonHoc.SelectedValue),
                    MaGV = ddlGiangVien.SelectedValue,
                    NgayHoc = DateTime.Parse(txtNgayHoc.Text),
                    CaHoc = int.Parse(ddlCaHoc.SelectedValue),
                    PhongHoc = int.Parse(ddlPhongHoc.SelectedValue),
                    MaLop = int.Parse(ddlLop.SelectedValue),
                   

                });
            
        }

        protected void btThemMoiTKB_Click(object sender, EventArgs e)
        {
            if (!KiemTraDuLieu())
            {
                if ((ddlCaHoc.SelectedValue.ToString() == "-1") || (ddlGiangVien.SelectedValue.ToString() == "-1") ||
                    (ddlMonHoc.SelectedValue.ToString() == "-1") || (ddlPhongHoc.SelectedValue.ToString() == "-1"))
                {
                    ThongBao("Bạn chưa chọn đủ dữ liệu");
                }
                else
                {
                    BTbTKB.Insert(new ETbTKB
                    {
                        MaMonHoc = int.Parse(ddlMonHoc.SelectedValue),
                        MaGV = ddlGiangVien.SelectedValue,
                        NgayHoc = DateTime.Parse(txtNgayHoc.Text),
                        CaHoc = int.Parse(ddlCaHoc.SelectedValue),
                        PhongHoc = int.Parse(ddlPhongHoc.SelectedValue),
                        MaLop = int.Parse(ddlLop.SelectedValue),
                        DiemDanh = 0
                    });
                }
            }
            LoadTKBTam();
            LoadTKB();
            Loanddl();
            Session["action"] = 1;
        }

        protected void gvTKBtam_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void gvTKBtam_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            
            try
            {
                int MaTKB = int.Parse(gvTKBtam.DataKeys[e.RowIndex].Value.ToString());
                BTbTKB.Delete(MaTKB);
                LoadTKBTam();
                LoadTKB();
                Session["action"] = 3;
            }
            catch
            {
                ThongBao("Dữ liệu liên quan đến thời khóa biểu này vẫn tồn tại, vui lòng kiểm tra lại dữ liệu!");
            }
        }

        protected void gvTKBtam_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < gvTKBtam.Rows.Count; i++)
                {
                    CheckBox cbChon = (CheckBox)gvTKBtam.Rows[i].Cells[0].FindControl("cbChon");
                    if (cbChon != null && cbChon.Checked)
                    {
                        BTbTKB.Delete(int.Parse(gvTKBtam.DataKeys[i].Value.ToString()));
                    }
                    else
                    {
                        ThongBao("Bạn chưa chọn thời khóa biểu nào để xóa!");
                    }
                }

                LoadTKB();
                Session["action"] = 3;
            }
            catch
            {
                ThongBao("Dữ liệu liên quan đến thời khóa biểu này vẫn tồn tại, vui lòng kiểm tra lại dữ liệu!");
            }
        }

        protected void btnLuu_Click1(object sender, EventArgs e)
        {
            for (int i = 0; i < gvTKBtam.Rows.Count; i++)
            {
                if (!KiemTraDuLieu())
                {
                    CheckBox cbChon = (CheckBox)gvTKBtam.Rows[i].Cells[0].FindControl("cbChon");
                    if (cbChon != null && cbChon.Checked)
                    {

                        BTbTKB.Update_State(new ETbTKB
                        {
                            MaTKB = int.Parse(gvTKBtam.DataKeys[i].Values[0].ToString()),
                            DiemDanh = 1
                        });

                    }
                    else
                    {
                        ThongBao("Bạn chưa chọn thời khóa biểu nào!");
                    }
                }
               

            }
            LoadTKBTam();
            LoadTKB();
        }

        protected void gvTKB_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int MaTKB = int.Parse(gvTKB.DataKeys[e.RowIndex].Value.ToString());
                BTbTKB.Delete(MaTKB);
                LoadTKBTam();
                LoadTKB();
                Session["action"] = 3;
            }
            catch
            {
                ThongBao("Dữ liệu liên quan đến thời khóa biểu này vẫn tồn tại, vui lòng kiểm tra lại dữ liệu!");
            }
        }
    }
}